/**
 * Word导入样式映射配置
 * 将Word文档中的样式名称映射到HTML标签和类名
 */

export interface WordStyleMapConfig {
  styleMap: string[];
  transformConvertRun?: (run: any) => any;
}

/**
 * 获取默认的Word样式映射配置
 * 这些配置用于mammoth库将Word样式转换为HTML
 */
export function getDefaultWordStyleMap(): WordStyleMapConfig {
  return {
    styleMap: [
      // 引用样式映射（使用更简单的格式，避免复杂的类名映射导致问题）
      "p[style-name='引用'] => blockquote:fresh",
      "p[style-name='blockquote'] => blockquote:fresh",
      "p[style-name='BlockQuote'] => blockquote:fresh",
      "p[style-name='引用样式'] => blockquote:fresh",
      "p[style-name='引用1'] => blockquote:fresh",
      "p[style-name='引用2'] => blockquote:fresh",
      
      // 代码块样式映射（使用更简单的格式）
      "p[style-name='代码块'] => pre:fresh",
      "p[style-name='Code'] => pre:fresh",
      "p[style-name='code'] => pre:fresh",
      "p[style-name='代码'] => pre:fresh",
      "p[style-name='CodeBlock'] => pre:fresh",
      "p[style-name='代码块1'] => pre:fresh",
      
      // 常见正文样式名称
      "p[style-name='Normal'] => p:fresh",
      "p[style-name='正文'] => p:fresh",
      "p[style-name='正文1'] => p:fresh",
      "p[style-name='正文2'] => p:fresh",
      
      // 标题样式映射（包括更多变体）
      "p[style-name='标题1'] => h1:fresh",
      "p[style-name='标题2'] => h2:fresh",
      "p[style-name='标题3'] => h3:fresh",
      "p[style-name='标题4'] => h4:fresh",
      "p[style-name='标题5'] => h5:fresh",
      "p[style-name='标题6'] => h6:fresh",
      "p[style-name='Heading 1'] => h1:fresh",
      "p[style-name='Heading 2'] => h2:fresh",
      "p[style-name='Heading 3'] => h3:fresh",
      "p[style-name='Heading 4'] => h4:fresh",
      "p[style-name='Heading 5'] => h5:fresh",
      "p[style-name='Heading 6'] => h6:fresh",
      "p[style-name='Heading1'] => h1:fresh",
      "p[style-name='Heading2'] => h2:fresh",
      "p[style-name='Heading3'] => h3:fresh",
      "p[style-name='Heading4'] => h4:fresh",
      "p[style-name='Heading5'] => h5:fresh",
      "p[style-name='Heading6'] => h6:fresh",
      "p[style-name='Title'] => h1:fresh",
      "p[style-name='Subtitle'] => h2:fresh",
      
      // 列表样式映射
      "p[style-name='List Paragraph'] => p:fresh",
      "p[style-name='列表段落'] => p:fresh",
      "p[style-name='List'] => p:fresh",
      "p[style-name='列表'] => p:fresh",
    ],
    
    // 转换运行（run）级别的样式
    transformConvertRun(run: any) {
      const resultRun: any = {}
      
      // 处理背景色（高亮）
      if ((run as any).bgColor) {
        resultRun['mark'] = {
          style: `background-color:${(run as any).bgColor}; color: inherit`,
          'data-color': (run as any).bgColor,
        }
      }
      
      // 处理删除线（strike）
      if ((run as any).strike || (run as any).strikethrough) {
        resultRun['s'] = {}
      }
      
      // 处理下划线（underline）
      if ((run as any).underline || (run as any).underlineColor) {
        resultRun['u'] = {}
      }
      
      // 处理上标（superscript）
      if ((run as any).vertAlign === 'superscript' || (run as any).vertAlign === 'superscript') {
        resultRun['sup'] = {}
      }
      
      // 处理下标（subscript）
      if ((run as any).vertAlign === 'subscript' || (run as any).vertAlign === 'subscript') {
        resultRun['sub'] = {}
      }
      
      return resultRun
    },
  }
}

/**
 * 获取自定义样式映射配置
 * @param customStyleMap 自定义样式映射数组
 * @param customTransformRun 自定义运行转换函数
 */
export function createWordStyleMap(
  customStyleMap?: string[],
  customTransformRun?: (run: any) => any
): WordStyleMapConfig {
  const defaultConfig = getDefaultWordStyleMap()
  
  return {
    styleMap: customStyleMap 
      ? [...defaultConfig.styleMap, ...customStyleMap]
      : defaultConfig.styleMap,
    transformConvertRun: customTransformRun || defaultConfig.transformConvertRun,
  }
}

